//
//  GGTButtonViewController.m
//  GGT
//
//  Created by 空空空 on 14/11/9.
//  Copyright (c) 2014年 GGT. All rights reserved.
//

#import "GGTButtonViewController.h"
#import "DPRequest.h"
#import "GGTAppDelegate.h"
#import "GGTTeamBuyModel.h"
#import "GGTTeamBuyTableViewCell.h"
#import "GGTTeamBuyDetailViewController.h"
#import "MBProgressHUD.h"

@interface GGTButtonViewController ()<UITableViewDataSource, UITableViewDelegate, DPRequestDelegate>
//页面三个导航button
@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UIButton *button3;
//主页面tableView
@property (nonatomic, retain) UITableView *tableView;
//保存数据用数组
@property (nonatomic, retain) NSMutableArray *array;
//MBProgressHUD
@property (nonatomic, retain) MBProgressHUD *hud;
@end

@implementation GGTButtonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.array = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建tableView
    [self filterButton];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height)];
    self.tableView.rowHeight = 120;
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [self.view addSubview:self.tableView];
//关于数据解析
    NSString *url = @"v1/deal/find_deals";
    NSString *params = [NSString stringWithFormat:@"keyword=%@&city=%@", self.str1, [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
    //HUB
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"刷新数据中...";
    [self.hud setMode:MBProgressHUDModeIndeterminate];
    
}
//创建页面导航button
- (void)filterButton
{
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button1.frame = CGRectMake(0, 64, self.view.frame.size.width / 3 - 1 , 30);
    self.button1.layer.cornerRadius = 10;
    self.button1.layer.borderWidth = 0.5;
    [self.button1 setTitle:self.str1 forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 addTarget:self action:@selector(button1Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button1];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button2.frame = CGRectMake(self.view.frame.size.width / 3 + 1, 64, self.view.frame.size.width / 3 - 1 , 30);
    self.button2.layer.cornerRadius = 10;
    self.button2.layer.borderWidth = 0.5;
    [self.button2 setTitle:self.str2 forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 addTarget:self action:@selector(button2Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button2];
    
    self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button3.frame = CGRectMake(self.view.frame.size.width / 3 * 2 + 2, 64, self.view.frame.size.width / 3 - 1 , 30);
    self.button3.layer.cornerRadius = 10;
    self.button3.layer.borderWidth = 0.5;
    [self.button3 setTitle:self.str3 forState:UIControlStateNormal];
    [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button3 addTarget:self action:@selector(button3Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button3];
    
}
#pragma mark - button相关点击事件
- (void)button1Click:(UIButton *)button
{
    //HUB
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"刷新数据中...";
    [self.hud setMode:MBProgressHUDModeIndeterminate];
    
    NSString *url = @"v1/deal/find_deals";
    NSString *params = [NSString stringWithFormat:@"keyword=%@&city=%@", self.str1, [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
}
- (void)button2Click:(UIButton *)button
{
    
    //HUB
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"刷新数据中...";
    [self.hud setMode:MBProgressHUDModeIndeterminate];
    
    NSLog(@"dianji");
    NSString *url = @"v1/deal/find_deals";
    NSString *params = [NSString stringWithFormat:@"keyword=%@&city=%@", self.str2, [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
}
- (void)button3Click:(UIButton *)button
{
    
    //HUB
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"刷新数据中...";
    [self.hud setMode:MBProgressHUDModeIndeterminate];
    
    NSString *url = @"v1/deal/find_deals";
    NSString *params = [NSString stringWithFormat:@"keyword=%@&city=%@", self.str3, [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"]];
    [[[GGTAppDelegate instance] dpapi] requestWithURL:url paramsString:params delegate:self];
}
#pragma mark - 豆瓣解析相关回调
- (void)request:(DPRequest *)request didFinishLoadingWithResult:(id)result
{
    [self.array removeAllObjects];

        NSMutableArray *arr = [result objectForKey:@"deals"];
        for (NSDictionary *dic in arr) {
            GGTTeamBuyModel *teamBuy = [[GGTTeamBuyModel alloc]initWithDictionary:dic];
            [self.array addObject:teamBuy];
        }
        [self.tableView reloadData];
    self.hud.hidden = YES;
}
#pragma mark - tableView相关回调
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GGTTeamBuyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ll"];
    if (cell == nil) {
        cell = [[[GGTTeamBuyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ll"]autorelease];
    }
    GGTTeamBuyModel *model = [self.array objectAtIndex:indexPath.row];
    cell.GGTTeamBuy = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GGTTeamBuyDetailViewController *detail = [[GGTTeamBuyDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    detail.GGTTeamBuyDetail = [self.array objectAtIndex:indexPath.row];
    [detail release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
